Engine.Texture = function(array, height, c){
    if(height!= undefined && c!= undefined){
        this.width = array;
        this.height = height;
        this.colors = c;
        return;
    }
    var getLine = function (begin,array)
    {
        var str = "";
        var i = begin;
        var temp;
        while((temp = String.fromCharCode(array[i++])) != "\n")
        {
            str += temp;
        }
        return {
            offset: i,
            line:str
        };
    };
    
    this.colors=[];
    var temp = getLine(0,array);
    temp = getLine(temp.offset,array);
    this.width = parseInt(temp.line);
    temp = getLine(temp.offset,array);
    this.height = parseInt(temp.line);
    temp = getLine(temp.offset,array);
    this.maxColor = parseInt(temp.line);
    
    for(var i = temp.offset; i < array.length ;)
    {
        this.colors.push(
            {
                r:array[i]/this.maxColor,
                g:array[i+1]/this.maxColor,
                b:array[i+2]/this.maxColor
            });
        i += 3;
    }
}

Engine.Texture.prototype = (function(){
    return {
        color : function(uv)
        {
            var u = uv.u.clamp(0,1) * (this.width-1);
            var v = (1-uv.v).clamp(0,1) * (this.height-1);
        
            var s = u - Math.floor(u);
            u = Math.floor(u);
            var t = v - Math.floor(v);
            v = Math.floor(v);
            if(u == this.width - 1)
            {
                u--;
                s++;
            }
            if(v == this.height - 1)
            {
                v--;
                t++;
            }

            var A = this.colors[u + v * this.width ];
            var B = this.colors[u + 1 + v * this.width ];
            var D = this.colors[u + (v + 1) * this.width ];
            var C = this.colors[u + 1 + (v + 1) * this.width ];
            
            return {
                r: s*t*C.r + (1-s)*t*D.r + s*(1-t)*B.r + (1-s)*(1-t)*A.r,
                g: s*t*C.g + (1-s)*t*D.g + s*(1-t)*B.g + (1-s)*(1-t)*A.g,
                b: s*t*C.b + (1-s)*t*D.b + s*(1-t)*B.b + (1-s)*(1-t)*A.b
            };
        },
		
		hatchColor : function(uv,c)
		{
			var returnColor={
				r:0,
				g:0,
				b:0
			}
			
			var u = uv.u.clamp(0,1) * (209);
			var v = (1-uv.v).clamp(0,1) * (209);
			
			u = Math.floor(u);
			v = Math.floor(v);
			
			
			for(var i = 0;i<6;i++)
			{
				var thisU = u + i*210;
				var thisV = v;
				
				returnColor.r += this.colors[thisU + thisV * this.width].r * c[i];
				returnColor.g += this.colors[thisU + thisV * this.width].g * c[i];
				returnColor.b += this.colors[thisU + thisV * this.width].b * c[i];

			}
			
			
			return returnColor;
			
		}
    };
})();